package com.qf.zyh.web.controller;

import com.qf.zyh.common.Const;
import com.qf.zyh.entity.Cart;
import com.qf.zyh.entity.Product;
import com.qf.zyh.entity.User;
import com.qf.zyh.services.CartSer;
import com.qf.zyh.services.Impl.CartSerImpl;
import com.qf.zyh.services.Impl.ProductSerImpl;
import com.qf.zyh.services.ProductSer;
import com.qf.zyh.util.StrUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/cart")
public class CartCont extends BaseServlet {
    CartSer cs = new CartSerImpl();
    public String addCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

            //获取id来获取对应id的手机数据，同时获取到用户的uid，来设置为哪个用户的购物车信息对应的是那些商品
            //然后江手机id用户id商品价格商品个数添加到购物车数据库中，发送到前端展示
            User user = (User) request.getSession().getAttribute(Const.UserCont.IS_LOGIN);
            //验证用户是否登录，如果没登录则重定向到登录
            if (user==null) {
            return Const.SysConst.REDIRECT+"login.jsp";
        }
        Integer pid = StrUtils.toint(request.getParameter("pid")) ;
        Boolean cart = cs.addCart(pid,user.getUid());
        //System.out.println(cart);
        String msg = "添加失败";
        if (cart){
            msg="添加成功";
        }
        request.setAttribute("msg",msg);
        return Const.SysConst.FORWARD+"cartSuccess.jsp";
    }
    public String seleCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        User user = (User) request.getSession().getAttribute(Const.UserCont.IS_LOGIN);
        //验证用户是否登录，如果没登录则重定向到登录
        if (user==null) {
            return Const.SysConst.REDIRECT+"login.jsp";
        }
        System.out.println(user);
        List<Cart> lc = cs.getAll(user.getUid());
            request.setAttribute("carts",lc);
            return Const.SysConst.FORWARD+"cart.jsp";
    }
    public String delecart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        try {
            User user = (User) request.getSession().getAttribute(Const.UserCont.IS_LOGIN);
            //验证用户是否登录，如果没登录则重定向到登录
            if (user==null) {
                return Const.SysConst.REDIRECT+"login.jsp";
            }
            String pid = request.getParameter("pid");
            Integer p = Integer.valueOf(pid);
            boolean b = cs.daleCart(p,user.getUid());

        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return Const.SysConst.REDIRECT+"cart?method=seleCart";
    }
    //前端使用ajax不需要返回值，直接设置数据库删除修改即可
    public void updateCartNum(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        User user = (User) request.getSession().getAttribute(Const.UserCont.IS_LOGIN);
        //验证用户是否登录，如果没登录则重定向到登录

        Integer pid = Integer.valueOf(request.getParameter("pid"));
        Integer num = Integer.valueOf(request.getParameter("num"));
        cs.updateCart(user.getUid(),pid,num);
    }
}
